﻿namespace SharePointRing.Controls
{
    using Framework.Helpers;
    using Framework.Model;
    using System.Windows;
    using System.Windows.Controls;

    public partial class NewWebApplication : UserControl
    {
        public NewWebApplication()
        {
            InitializeComponent();
        }

        private void CreateWebApplication(object sender, RoutedEventArgs e)
        {
            var webApplicationInfo = new WebApplicationInfo();
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            SPFarmHelper.GetAllManagedAccounts();
            PortTextBox.Text = SPWebApplicationHelper.GetRandomAvailablePort().ToString();
            DatabaseServerNameTextBox.Text = SPFarmHelper.GetDefaultContentDatabaseServer();

            var appPools = SPFarmHelper.GetAllApplicationPools();
            foreach (var appPool in appPools)
            {
                ApplicationPoolsComboBox.Items.Add(appPool);
            }

            var managedAccounts = SPFarmHelper.GetAllManagedAccounts();
            foreach (var managedAccount in managedAccounts)
            {
                ManagedAccountsComboBox.Items.Add(managedAccount);
            }
        }

        private void PortTextBox_TextChanged(object sender, System.Windows.Controls.TextChangedEventArgs e)
        {
            DatabaseNameTextBox.Text = "WSS_Content_" + PortTextBox.Text;
            IisPathTextBox.Text = SPFarmHelper.GetBaseIisPath() + PortTextBox.Text;
            NameTextBox.Text = "SharePoint - " + PortTextBox.Text;
            ApplicationPoolIdentityTextBox.Text = "SharePoint - " + PortTextBox.Text;
        }

        private void CheckBox_Checked_1(object sender, RoutedEventArgs e)
        {
            ApplicationPoolsComboBox.IsEnabled = !CreateNewAppPoolCheckBox.IsChecked.Value;
            ApplicationPoolIdentityTextBox.IsEnabled = CreateNewAppPoolCheckBox.IsChecked.Value;
            ApplicationPoolUserNameTextBox.IsEnabled = CreateNewAppPoolCheckBox.IsChecked.Value;
            ApplicationPoolPasswordTextBox.IsEnabled = CreateNewAppPoolCheckBox.IsChecked.Value;
        }

        private void ManagedAccountsComboBox_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
        {
            ApplicationPoolUserNameTextBox.Text = ManagedAccountsComboBox.SelectedValue.ToString();
        }

        private void ApplicationPoolsComboBox_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
        {
            var info = (ApplicationPoolInfo)ApplicationPoolsComboBox.SelectedValue;
            ApplicationPoolIdentityTextBox.Text = info.Name;
            ApplicationPoolUserNameTextBox.Text = info.Username;
            ApplicationPoolPasswordTextBox.Password = "...........";
        }
    }
}
